function DetailProductService() { }

DetailProductService.prototype = {

    ROOT_URL: 'https://wbc-atodekau.appspot.com/_ah/api/apiProxyEndpoint/v2/',
    
    DEVICE_ID: -1,
    
    SERVER_LOG_SCAN: 10,
    SERVER_LOG_BOOKMARK: 20,
    SERVER_LOG_DELETE: 30,
    
    /**
     * @author: Hai Do Minh
     * @params:
     *  - action: 
     *      (
     *        + Find list shops by barcode: "findbybarcode"
     *        + Find detail item by barcode: "prodbybarcode"
     *        + Find dlist items by keyword searching: "findbykeyword"
     *        + Find detail item by item_id: "prodbyitemid"
     *      )
     * 
     * + Find detail item or list shops by barcode
     *  - barcode:
     * 
     * + Find detail item by its item_id
     *  - eccode:
     *  - itemid: 
     * 
     * + Find list items by keyword searching 
     *  - options {keyword: '', page: 1, minPrice: 1, maxPrice: 999999999, creditCardFlag: 0, postageFlag: 0}
     */ 
    getItemInfo : function(params, success, error) {
        var url = DetailProductService.prototype.ROOT_URL;
        var deviceParam = false;
        switch(params.action) {
            case "findbybarcode":
                url += "findByBarcode/" + params.barcode;
                break;
            case "prodbybarcode":
                url += "findProductInfoByBarcode/" + params.barcode;
                break;
            case "findbykeyword":
                url += 'findItems?hits=' + Util.prototype.maxBookmarkNumber;
                for (var key in params.options) {
                    var value = params.options[key];
                    if (typeof value != 'undefined' && value != '') {
                        url += "&" + key + "=" + ((key === "keyword" || key === "NGKeyword") ? encodeURIComponent(value) : value);
                               
                    }         
                }
                deviceParam = true;
                break;
            case "prodbyitemid":
                url += "findItems?hits=1&ecsite_code=" + params.eccode + "&itemCode=" + params.itemid;
                deviceParam = true;
                break;
            default:
                callbackError();
                return false;
        }
        DetailProductService.prototype.connectToServer(url, success, error, deviceParam);
    },

    getEcNameByECcode : function(ecCode) {
        switch (ecCode) {
            case 10:
                return "楽天市場";
            default:
                return "店名";
        }
    },
    
    getGenreByGenreIdOriginal : function(ec_code, genreOriginalId, success, error) {
        var url = DetailProductService.prototype.ROOT_URL + "doGenreMatching?ecsite_code=" + ec_code + 
                    "&genreIdOriginal=" + genreOriginalId;
        DetailProductService.prototype.connectToServer(url, success, error, true);
    },
    
    saveLog : function(data, bmid, item) {
        if (typeof bmid != 'undefined') {
            data = DetailProductService.prototype.genDataLog(data, bmid, item);
        }
        data.deviceId = DetailProductService.prototype.DEVICE_ID;
        console.log("data log: " + JSON.stringify(data));
        $.ajax({
            type: 'POST',
            url: DetailProductService.prototype.ROOT_URL+'saveLog',
            dataType: 'json',
            data: data,
            success: function(result) {
                console.log('LOG: ' + data.log_type + ": success");
            },
            error: function(ex) {
                console.log('LOG: ' + data.log_type + ": error: " + JSON.stringify(ex));
            }
        });
    },
    
    genDataLog: function(logType, bmid, item) {
        var registerDate = Util.prototype.getTodayObj();        
        var data = { "log_type": logType,
                        "barcode_number": item.barcode_number,
                        "item_name": item.item_name,
                        "ecsite_code": item.ecsite_code,
                        "item_id": item.item_id,
                        "image_url": item.image_url,
                        "genre_code":  item.genre_code,
                        "prefecure_code": item.prefecture_code,
                        "pref_no": item.pref_no,
                        "longitude": item.longitude,
                        "latitude": item.latitude,
                        "registered_date": registerDate.ymd + registerDate.hms
                    };
        if (bmid != null) {
            data.bookmarkId = bmid;
        }   
        
        return data;    
    },
    
    connectToServer: function(url, success, error, deviceParam) {
        var devicedUrl = url + (deviceParam ? 
                                  ("&deviceId=" + DetailProductService.prototype.DEVICE_ID) : 
                                  ('/' + DetailProductService.prototype.DEVICE_ID));
        Util.prototype.serverConnecting = true;
        $.ajax({
            type: 'GET',
            url: devicedUrl,
            dataType: 'json',
            success: function(result) {
                Util.prototype.serverConnecting = false;
                success(result);
            },
            error: function() {
                Util.prototype.serverConnecting = false;
                error();
            }
        });
    }
 
};